package it.tukano.treditor.engine.jme3;

import com.jme3.input.MouseInput;
import com.jme3.input.RawInputListener;
import com.jme3.input.event.JoyAxisEvent;
import com.jme3.input.event.JoyButtonEvent;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.input.event.TouchEvent;
import com.jme3.renderer.Camera;
import it.tukano.treditor.engine.input.EngineInputListener;
import java.util.HashMap;
import java.util.Map;

public class EngineInputHandler implements RawInputListener {
    
    private final Map<Integer, Long> lastButtonPressTimes = new HashMap<Integer, Long>();
    private final EngineInputListener callback;
    private final long clickDeltaMs = 250;
    private final Camera canvas;

    /**
     * Instance initializer
     */
    public EngineInputHandler(EngineInputListener callback, Camera camera) {
        this.callback = callback;
        this.canvas = camera;
    }

    @Override
    public void beginInput() {
    }

    @Override
    public void endInput() {
    }

    @Override
    public void onJoyAxisEvent(JoyAxisEvent e) {
    }

    @Override
    public void onJoyButtonEvent(JoyButtonEvent e) {
    }

    @Override
    public void onMouseMotionEvent(MouseMotionEvent e) {
    }

    @Override
    public void onMouseButtonEvent(MouseButtonEvent e) {
        int button = e.getButtonIndex();
        int x = e.getX();
        int y = e.getY();
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        long now = System.currentTimeMillis();
        long then = getLastPressedTime(button);
        long delta = now - then;
        boolean clickTimeElapsed = delta > clickDeltaMs;
        boolean wasDown = then != -1;
        boolean wasUp = then == -1;
        boolean isDown = e.isPressed();
        boolean isUp = e.isReleased();
        boolean isLeft = button == MouseInput.BUTTON_LEFT;
        boolean isRight = button == MouseInput.BUTTON_RIGHT;
        boolean isMiddle = button == MouseInput.BUTTON_MIDDLE;
        if(wasDown && isUp && !clickTimeElapsed) {
            if(isLeft) {
                callback.onLeftClick(x, height - y);
            } else if(isRight) {
                callback.onRightClick(x, height - y);
            } else if(isMiddle) {
                callback.onMiddleClick(x, height - y);
            }
        } else if(wasDown && isUp) {
            if(isLeft) {
                callback.onLeftDown(x, height - y);
            } else if(isRight) {
                callback.onRightDown(x, height - y);
            } else if(isMiddle) {
                callback.onMiddleDown(x, height - y);
            }
        } else if(wasUp && isDown) {
            if(isLeft) {
                callback.onLeftUp(x, height - y);
            } else if(isRight) {
                callback.onRightUp(x, height - y);
            } else if(isMiddle) {
                callback.onMiddleUp(x, height - y);
            }
        }
        setLastPressedTime(button, isDown ? now : -1);
    }
    
    public void setLastPressedTime(int button, long time) {
        lastButtonPressTimes.put(button, time);
    }
    
    public long getLastPressedTime(int button) {
        Long time = lastButtonPressTimes.get(button);
        return time == null ? -1 : time;
    }

    @Override
    public void onKeyEvent(KeyInputEvent evt) {
    }

    @Override
    public void onTouchEvent(TouchEvent evt) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
